<?php
// +----------------------------------------------------------------------
// | 控制器 - ADMIN - 系统管理
// +----------------------------------------------------------------------
declare(strict_types=1);

namespace app\controller\admin\system;

use OpenApi\Annotations as OA;
use app\BaseController;
use app\model\SystemDeptsModel;
use app\validate\SystemDeptsValidate;
use think\exception\ValidateException;
use think\Response;

/**
 * 部门管理
 *
 * Class Depts
 * @package app\controller\admin\system
 */
class Depts extends BaseController
{

    /**
     * @OA\Get(
     *   path="/api/admin/system/depts",
     *   tags={"系统部门"},
     *   summary="获取部门列表",
     *   @OA\Parameter(description="类型检索", in="query", name="type", @OA\Schema(type="string")),
     *   @OA\Parameter(description="部门名称检索", in="query", name="deptName", @OA\Schema(type="string")),
     *   @OA\Parameter(description="部门负责人检索", in="query", name="principal", @OA\Schema(type="string")),
     *   @OA\Parameter(description="手机检索", in="query", name="phone", @OA\Schema(type="string")),
     *   @OA\Parameter(description="邮箱检索", in="query", name="email", @OA\Schema(type="string")),
     *   @OA\Parameter(description="备注检索", in="query", name="remark", @OA\Schema(type="string")),
     *   @OA\Parameter(description="状态检索，可传入true或false", in="query", name="status", @OA\Schema(type="string")),
     *   security={{"bearerAuth":{}}},
     *   @OA\Response(response=401, description="未携带TOKEN或已过期"),
     *   @OA\Response(
     *     response="200",
     *     description="Success",
     *     @OA\JsonContent(
     *       type="object",
     *       @OA\Property(property="success", type="boolean", example=true),
     *       @OA\Property(property="message", type="string", example="操作成功"),
     *       @OA\Property(
     *         property="data",
     *         type="array",
     *         @OA\Items(ref="#/components/schemas/GetSystemDepts")
     *       )
     *     )
     *   )
     * )
     * @return Response
     */
    public function getList(): Response
    {
        // 处理查询数据
        $search = [
            'deptName' => $this->request->get('deptName'),
            'principal' => $this->request->get('principal'),
            'phone' => $this->request->get('phone'),
            'email' => $this->request->get('email'),
            'remark' => $this->request->get('remark'),
            'status' => $this->request->get('status'),
        ];
        $list = SystemDeptsModel::queryOrder()->withSearch(array_keys($search), $search)->select();
        return $this->response($list->toArray());
    }

    /**
     * @OA\Post(
     *   path="/api/admin/system/depts",
     *   tags={"系统部门"},
     *   summary="创建部门信息",
     *   @OA\RequestBody(
     *     required=true,
     *     @OA\JsonContent(
     *       type="object",
     *       ref="#/components/schemas/SetSystemDepts"
     *     )
     *   ),
     *   security={{"bearerAuth":{}}},
     *   @OA\Response(response=401, description="未携带TOKEN或已过期"),
     *   @OA\Response(
     *     response="200",
     *     description="Success",
     *     @OA\JsonContent(
     *       type="object",
     *       @OA\Property(property="success", type="boolean", example=true),
     *       @OA\Property(property="message", type="string", example="操作成功"),
     *       @OA\Property(
     *         property="data",
     *         type="object",
     *         ref="#/components/schemas/GetSystemDepts"
     *       )
     *     )
     *   )
     * )
     * @return Response
     */
    public function create(): Response
    {
        $query = $this->request->post();
        try {
            // 字段验证
            validate(SystemDeptsValidate::class)->check($query);
            // 数据新增
            $data = new SystemDeptsModel();
            $data->save($query);
            // 返回新数据
            return $this->response($data->toArray());
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            abort(200, $e->getError());
        }
    }

    /**
     * @OA\Get(
     *   path="/api/admin/system/depts/{id}",
     *   tags={"系统部门"},
     *   summary="获取部门信息",
     *   @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")),
     *   security={{"bearerAuth":{}}},
     *   @OA\Response(response=401, description="未携带TOKEN或已过期"),
     *   @OA\Response(
     *     response="200",
     *     description="Success",
     *     @OA\JsonContent(
     *       type="object",
     *       @OA\Property(property="success", type="boolean", example=true),
     *       @OA\Property(property="message", type="string", example="操作成功"),
     *       @OA\Property(
     *         property="data",
     *         type="object",
     *         ref="#/components/schemas/GetSystemDepts"
     *       )
     *     )
     *   )
     * )
     * @return Response
     */
    public function read(): Response
    {
        $id = $this->request->route('id');
        $data = SystemDeptsModel::queryOrder()->find($id);
        if (!$data) {
            abort(404, '数据未找到');
        }
        return $this->response($data->toArray());
    }

    /**
     * @OA\Put(
     *   path="/api/admin/system/depts/{id}",
     *   tags={"系统部门"},
     *   summary="更新部门信息",
     *   @OA\Parameter(name="id", in="path", required=true,@OA\Schema(type="integer")),
     *   @OA\RequestBody(
     *     required=true,
     *     @OA\JsonContent(
     *       type="object",
     *       ref="#/components/schemas/SetSystemDepts"
     *     )
     *   ),
     *   security={{"bearerAuth":{}}},
     *   @OA\Response(response=401, description="未携带TOKEN或已过期"),
     *   @OA\Response(
     *     response="200",
     *     description="Success",
     *     @OA\JsonContent(
     *       type="object",
     *       @OA\Property(property="success", type="boolean", example=true),
     *       @OA\Property(property="message", type="string", example="操作成功"),
     *       @OA\Property(
     *         property="data",
     *         type="object",
     *         ref="#/components/schemas/GetSystemDepts"
     *       )
     *     )
     *   )
     * )
     * @return Response
     */
    public function update(): Response
    {
        $id = $this->request->route('id');
        $query = $this->request->put();
        try {
            $data = SystemDeptsModel::find($id);
            if (!$data) {
                abort(404, '数据未找到');
            }
            // 字段验证
            validate(SystemDeptsValidate::class)->check($query);
            // 数据更新
            $data->save($query);
            // 返回新数据
            return $this->response($data->toArray());
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            abort(200, $e->getError());
        }
    }

    /**
     * @OA\Put(
     *   path="/api/admin/system/depts/{id}/status",
     *   tags={"系统部门"},
     *   summary="切换部门状态信息",
     *   @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")),
     *   @OA\RequestBody(
     *     required=true,
     *     @OA\JsonContent(
     *       type="object",
     *       @OA\Property(property="status", type="boolean", example=true),
     *     )
     *   ),
     *   security={{"bearerAuth":{}}},
     *   @OA\Response(response=401, description="未携带TOKEN或已过期"),
     *   @OA\Response(
     *     response="200",
     *     description="Success",
     *     @OA\JsonContent(
     *       type="object",
     *       @OA\Property(property="success", type="boolean", example=true),
     *       @OA\Property(property="message", type="string", example="操作成功"),
     *       @OA\Property(property="data", example=null)
     *     )
     *   )
     * )
     * @return Response
     */
    public function setStatus(): Response
    {
        $id = $this->request->route('id');
        $query = [
            'status' => $this->request->put('status')
        ];
        $data = SystemDeptsModel::find($id);
        if (!$data) {
            abort(404, '数据未找到');
        }
        // 数据更新
        $data->save($query);
        return $this->response();
    }

    /**
     * @OA\Delete(
     *   path="/api/admin/system/depts/{id}",
     *   tags={"系统部门"},
     *   summary="删除部门信息",
     *   @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")),
     *   security={{"bearerAuth":{}}},
     *   @OA\Response(response=401, description="未携带TOKEN或已过期"),
     *   @OA\Response(
     *     response="200",
     *     description="Success",
     *     @OA\JsonContent(
     *       type="object",
     *       @OA\Property(property="success", type="boolean", example=true),
     *       @OA\Property(property="message", type="string", example="操作成功"),
     *       @OA\Property(property="data", example=null)
     *     )
     *   )
     * )
     * @return Response
     */
    public function delete(): Response
    {
        $id = $this->request->route('id');
        $data = SystemDeptsModel::find($id);
        if (!$data) {
            abort(404, '数据未找到');
        }
        $data->delete();
        return $this->response();
    }

    /**
     * @OA\Delete(
     *   path="/api/admin/system/depts",
     *   tags={"系统部门"},
     *   summary="批量删除部门信息",
     *   @OA\RequestBody(
     *   @OA\MediaType(
     *     mediaType="content-type/json",
     *       @OA\Schema(
     *         @OA\Property(
     *           description="指定的id",
     *           property="ids",
     *           type="array",
     *           items={@OA\Items(type="integer")},
     *           example={1, 2}
     *         ),
     *         required={"ids"})
     *     )
     *   ),
     *   security={{"bearerAuth":{}}},
     *   @OA\Response(response=401, description="未携带TOKEN或已过期"),
     *   @OA\Response(
     *     response="200",
     *     description="Success",
     *     @OA\JsonContent(
     *       type="object",
     *       @OA\Property(property="success", type="boolean", example=true),
     *       @OA\Property(property="message", type="string", example="操作成功"),
     *       @OA\Property(property="data", example=null)
     *     )
     *   )
     * )
     * @return Response
     */
    public function batchDelete(): Response
    {
        $ids = $this->request->delete('ids');
        if (!$ids || !is_array($ids)) {
            abort(404, '数据未找到');
        }
        SystemDeptsModel::destroy($ids);
        return $this->response();
    }
}
